<script setup>
import { ref, reactive, onMounted, nextTick, onBeforeUnmount } from 'vue';
import { ElMessage } from 'element-plus';
import { getMenuParams, getLastdata } from '@T2/utils/params.ts';
import { useRoute } from 'vue-router';
import DataEcharts from '../components/dataEcharts.vue';
import dayjs from 'dayjs';

const route = useRoute();

onMounted(() => {
  refreshData();
});

const Rt = reactive({
  updateTimes: 0,
  Overview: null,
  WaterCurve: null,
  Rundata: null,
  RunCurve: null,
});
const timer = setInterval(() => {
  Rt.updateTimes++;
  refreshData();
}, 20000);
const refreshData = async () => {
  const { sensors } = getMenuParams(route.path);
  if (!sensors?.length) {
    ElMessage.error('未配置参数！');
    return;
  }
  const { curIds, diffIds } = await getLastdata(sensors);
  const [sensors0, sensors1, sensors2, sensors3] = sensors || [];
  Rt.Overview = sensors0, Rt.WaterCurve = sensors1, Rt.Rundata = sensors2, Rt.RunCurve = sensors3;
}

const domRt = reactive({
  waters: [],
  water_dex: 0,
  runs: [],
  run_dex: 0,
});
const WaterCurveClick = (index) => {
  getWatersDom();
  domRt.water_dex = index;
  domRt.waters[domRt.water_dex].scrollIntoView({inline: 'center'});
}
const WaterCurveGoFront = () => {
  getWatersDom();
  domRt.water_dex = domRt.water_dex >= 1 ? domRt.water_dex - 1 : 0;
  domRt.waters[domRt.water_dex].scrollIntoView({inline: 'center'});
}
const WaterCurveGoAfter = () => {
  getWatersDom();
  domRt.water_dex = domRt.water_dex <= Rt.WaterCurve.c.length - 2 ? domRt.water_dex + 1 : WaterCurve.value.c.length - 1;
  domRt.waters[domRt.water_dex].scrollIntoView({inline: 'center'});
}
const getWatersDom = () => {
  if (domRt.waters.length) {
    return;
  }
  domRt.waters = document.querySelectorAll('.WaterCurve>.tabLine>.tabList>.tabItem');
}

function RunCurveClick(index) {
  getRunsDom();
  domRt.run_dex = index;
  domRt.runs[domRt.run_dex].scrollIntoView({inline: 'center'});
}
function RunCurveGoFront() {
  getRunsDom();
  domRt.run_dex = domRt.run_dex >= 1 ? domRt.run_dex - 1 : 0;
  domRt.runs[domRt.run_dex].scrollIntoView({inline: 'center'});
}
function RunCurveGoAfter() {
  getRunsDom();
  domRt.run_dex = domRt.run_dex <= Rt.RunCurve.c.length - 2 ? domRt.run_dex + 1 : Rt.RunCurve.c.length - 1;
  domRt.runs[domRt.run_dex].scrollIntoView({inline: 'center'});
}
const getRunsDom = () => {
  if (domRt.runs.length) {
    return;
  }
  domRt.runs = document.querySelectorAll('.RunCurve>.tabLine>.tabList>.tabItem');
}

onBeforeUnmount(() => {
  clearInterval(timer);
});
</script>

<template>
  <div class="left flex-column">
    <template v-if="Rt.Overview?.c?.length">
      <div class="title">{{ Rt.Overview.n }}</div>
      <div class="databox overview flex flex-wrap">
        <div class="item" v-for="(item, index) in Rt.Overview.c" :key="index">
          <div class="data_nums">{{ item.value }}</div>
          <span class="data_name">{{ item.n }}({{ item.unit }})</span>
        </div>
      </div>
    </template>
    
    <template v-if="Rt.WaterCurve?.c?.length">
      <div class="title">{{ Rt.WaterCurve.n }}</div>
      <div class="databox WaterCurve">
        <div class="tabLine flex-sb-center">
          <el-icon @click="WaterCurveGoFront()"><ArrowLeft /></el-icon>
          <div class="tabList flex--center flex-nowrap">
            <div
              v-for="(item, index) in Rt.WaterCurve.c"
              class="tabItem"
              :class="{'isActive': domRt.water_dex === index}"
              @click="WaterCurveClick(index)"
            >
              {{ item.n }}
            </div>
          </div>
          <el-icon @click="WaterCurveGoAfter()"><ArrowRight /></el-icon>
        </div>
        <div class="charts-box">
          <DataEcharts
            v-if="Rt.WaterCurve.c[domRt.water_dex].c.length"
            :groups="Rt.WaterCurve.c[domRt.water_dex].c"
            :groupDex="domRt.water_dex"
          />
        </div>
      </div>
    </template>
  </div>
  <div class="right flex-column">
    <template v-if="Rt.Rundata?.c?.length">
      <div class="title">{{ Rt.Rundata.n }}</div>
      <div class="databox rundata flex flex-wrap">
        <div class="item" v-for="(item, index) in Rt.Rundata.c">
          <div class="data_nums">{{ item.value }}</div>
          <div class="data_bg flex-center-center flex-column">
            <span class="data_name">{{ item.n }}</span>
            <span class="data_name">（{{ item.unit }}）</span>
          </div>
        </div>
      </div>
    </template>
    
    <template v-if="Rt.RunCurve?.c?.length">
      <div class="title">运行趋势</div>
      <div class="databox RunCurve">
        <div class="tabLine flex-sb-center">
          <el-icon @click="RunCurveGoFront()"><ArrowLeft /></el-icon>
          <div class="tabList flex--center flex-nowrap">
            <div
              class="tabItem"
              :class="{'isActive': domRt.run_dex === index}"
              v-for="(item, index) in Rt.RunCurve.c"
              @click="RunCurveClick(index)"
            >
              {{ item.n }}
            </div>
          </div>
          <el-icon @click="RunCurveGoAfter()"><ArrowRight /></el-icon>
        </div>
        <DataEcharts
          v-if="Rt.RunCurve.c[domRt.run_dex].c.length"
          :groups="Rt.RunCurve.c[domRt.run_dex].c"
          :groupDex="domRt.run_dex"
        />
      </div>
    </template>
  </div>
</template>

<style lang="scss" scoped>
@font-face {
  font-family: "YouSheBiaoTiHei";
  src: url("/font/优设标题黑.ttf");
}
.left,
.right {
  position: absolute;
  top: 0;
  width: 450px;
  height: 100%;
  padding-top: 18px;
  .title {
    width: 336px;
    height: 41px;
    line-height: 41px;
    background: url('../assets/data/title_bg.png') no-repeat center;
    background-size: 100% 100%;
    margin: 30px 0 15px;
    font-size: 20px;
    font-family: YouSheBiaoTiHei;
    color: #D3F2EE;
    padding-left: 31px;
  }
  .databox {
    width: 340px;
    min-height: 302px;
    position: relative;
  }
  .data_nums {
    font-size: 24px;
    font-family: YouSheBiaoTiHei;
    line-height: 31px;
    color: #10FFF9;
    @supports (-webkit-background-clip: text) or (background-clip: text) {
      background: linear-gradient(180deg, #E2FFFE 0%, #0AC4C0 100%);
      background-clip: text;
      -webkit-background-clip: text;
      color: transparent;
    }
  }
  .data_name {
    font-size: 12px;
    font-family: PingFangSC;
    font-weight: 500;
    color: #B2B2B2;
    margin-left: 7px;
    line-height: 17px;
  }
  .overview {
    padding: 12px 10px 23px;
    .item {
      width: 50%;
      height: 53px;
      margin: 18px 0;
      .data_nums {
        position: relative;
        padding-left: 34px;
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          width: 34px;
          height: 33px;
          background: url('../assets/data/front.png') no-repeat center;
          background-size: 100% 100%;
        }
      }
    }
  }
  .WaterCurve,
  .RunCurve {
    padding: 18px 0 12px;
    .tabLine {
      padding: 0 12px;
      width: 100%;
      margin-bottom: 18px;
      .tabList {
        width: fit-content;
        max-width: calc(100% - 32px);
        overflow: hidden;
        margin: 0 4px;
        height: 24px;
        border: 1px solid rgba(16,255,249,0.5);
        background: linear-gradient(180deg, rgba(3,95,102,0.6) 0%, rgba(3,95,102,0) 100%);
        border-radius: 3px;
        .tabItem {
          width: fit-content;
          min-width: fit-content;
          height: 22px;
          line-height: 22px;
          padding: 0 12px;
          font-size: 12px;
          font-family: PingFangSC;
          font-weight: 500;
          color: rgba(16,255,249,0.7);
          cursor: pointer;
          border: 1px solid rgba(0,0,0,0);
          position: relative;
          &::after {
            content: ' ';
            position: absolute;
            // color: ;
            width: 0;
            height: 14px;
            right: -2px;
            top: 50%;
            transform: translateY(-50%);
            border: 1px solid;
            border-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.6), rgba(255, 255, 255, 0)) 1 1;
          }
        }
        .tabItem:nth-last-child(1) {
          &::after {
            border: none;
          }
        }
        .isActive {
          background: linear-gradient(180deg, rgba(16,255,249,0.6) 0%, rgba(3,95,102,0) 100%);
          border-radius: 3px;
          border: 1px solid #10FFF9;
          color: #D3F2EE;
          &::after {
            border: none;
          }
        }
      }
    }
    .charts-box {
      margin-top: 12px;
      width: 100%;
      height: 230px;
      position: relative;
    }
  }
  .rundata {
    padding: 11px 0 12px;
    .item {
      width: 50%;
      height: 101px;
      margin: 19px 0;
      text-align: center;
      position: relative;
      .data_bg {
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 84px;
        height: 82px;
        background: url('../assets/data/bg.png') no-repeat center;
        background-size: 100% 100%;
      }
      .data_name {
        color: rgba(255,255,255,0.8);
      }
    }
  }
}
.left {
  left: 0;
  background: linear-gradient(90deg, rgba(0,0,0,0.8) 0%, rgba(0,0,0,0.4) 80%, rgba(0,0,0,0) 100%);
  padding-left: 34px;
  animation: lEnter 1s linear 0s;
  -webkit-animation: lEnter 1s linear 0s;
  animation-fill-mode: backwards;
  -webkit-animation-fill-mode: backwards;
  .databox {
    background: url('../assets/data/box_bg.png') no-repeat center;
    background-size: 100% 100%;
  }
}
.right {
  right: 0;
  background: linear-gradient(90deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.4) 20%, rgba(0,0,0,0.8) 100%);
  padding-right: 34px;
  align-items: flex-end;
  animation: rEnter 1s linear 0s;
  -webkit-animation: rEnter 1s linear 0s;
  animation-fill-mode: backwards;
  -webkit-animation-fill-mode: backwards;
  .databox::before {
    content: ' ';
    position: absolute;
    left: 0;
    top: 0;
    background: url('../assets/data/box_bg.png') no-repeat center;
    background-size: 100% 100%;
    width: 100%;
    height: 100%;
    transform: rotate(180deg);
  }
}
@keyframes lEnter {
  0% {
    transform: translateX(-450px);
    opacity: 0;
  }
  100% {
    transform: translateX(0px);
    opacity: 1;
  }
}
@keyframes rEnter {
  0% {
    transform: translateX(450px);
    opacity: 0;
  }
  100% {
    transform: translateX(0px);
    opacity: 1;
  }
}
</style>

<style scoped>
:deep(.tabLine .el-icon) {
  border-radius: 50%;
  border: none;
  width: 16px;
  height: 16px;
  background: linear-gradient(180deg, #0B888C 0%, #035F66 100%);
  box-shadow: inset 0px 2px 4px 0px rgba(11,136,140,0.44);
  padding: 2px;
  cursor: pointer;
  z-index: 0;
}
:deep(.tabLine .el-icon:hover) {
  background: linear-gradient(180deg, #D3F2EE 0%, #5CCCC5 100%);
}
:deep(.tabLine .el-icon svg) {
  width: 12px;
  height: 12px;
  color: #000 !important;
}
</style>